CFLAGS = -g -Wall -Wextra $(OFLAGS) $(XFLAGS)
OFLAGS = -DDEBUG
#OFLAGS = -pg

OBJS = tree.o compile.o

all : gml

gml:
	rm -v -f ./gml.c
	rm -v -f ./log.txt
	./leg -o gml.c gml.leg
	$(CC) $(CFLAGS) gml.c -o gml
	cat advent.gml |./gml 2>log.txt



leg : leg.o $(OBJS)
	$(CC) $(CFLAGS) -o $@-new leg.o $(OBJS)
	mv $@-new $@
	mv leg.c leg.c.bak
	rm -v -f *.o
	./leg -o leg.c leg.leg
	$(CC) $(CFLAGS) -c leg.c compile.c tree.c
	$(CC) $(CFLAGS) -o $@-new leg.o $(OBJS)
	mv $@-new $@
	make basic


ROOT	=
PREFIX	= /usr/local
BINDIR	= $(ROOT)$(PREFIX)/bin

install : $(BINDIR)/leg

$(BINDIR)/% : %
	cp -p $< $@
	strip $@

uninstall : .FORCE
	rm -f $(BINDIR)/peg
	rm -f $(BINDIR)/leg


leg.o : leg.c

leg.c : leg.leg
#	./leg -o $@ $<

check : leg .FORCE
	./leg < leg.leg > leg.out
	rm -v -f diff.leg
	diff leg.c leg.out >leg.diff
	rm leg.out

test examples : .FORCE

basic:	basic.leg
	rm -v -f ./basic.c
	./leg -o basic.c basic.leg
	$(CC) $(CFLAGS) basic.c -o basic
	./basic test.bas

v1:
	cp leg-0.1.1.c leg.c
	cp leg.leg-0.1.1 leg.leg
	cp compile-0.1.1.c compile.c
	cp tree-0.1.1.c tree.c
	cp tree-0.1.1.h tree.h
	make cleaner

v18:
	cp leg-0.1.18.c leg.c
	cp leg.leg-0.1.18 leg.leg
	cp compile-0.1.18.c compile.c
	cp tree-0.1.18.c tree.c
	cp tree-0.1.18.h tree.h
	make cleaner

clean : .FORCE
	rm -f *~ *.o  *.leg.[cd]
	rm -v -f gml
	rm -v -f gml.c

cleaner:
	make spotless

spotless : clean .FORCE
	rm -v -f ./leg

.FORCE :
